package com.wkck.controller.frontend;

import cn.hutool.core.lang.mutable.MutableInt;
import com.wkck.VO.UserResumesInfoUnIncludeOnlineResumesVo;
import com.wkck.VO.UserResumesInfoVo;
import com.wkck.domain.*;
import com.wkck.dto.ResumeHobbyDto;
import com.wkck.dto.ResumeHonorAwardDto;
import com.wkck.result.Result;
import com.wkck.service.LResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.message.ReusableMessage;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @Author: CK
 * @Date: 2024/03/01/20:36
 * @Description:
 */
@Api(value = "用户简历Api", tags = "用户简历Api")
@RestController
@RequestMapping("/user/resume")
@Slf4j
@CrossOrigin
public class LResumeFrontendController {

    @Autowired
    private LResumeService resumeService;

    /***
    * @Author: CK
    * @Date: 2024/3/1
    * @Param: [id]
    * @Description: 根据用户id得到所有已存简历信息
    */
    @GetMapping("/list/{id}")
    @ApiOperation(value = "根据用户id得到所有已存简历信息")
    public Result<UserResumesInfoVo> getUserResumesById(@PathVariable("id") Integer id) {
        log.info("获取简历信息用户id =====> {}", id);
        return Result.success(resumeService.getUserResumesById(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/7
    * @Param: [resumeId, userId]
    * @Description: 根据用户id和简历id得到简历信息
    */
    @GetMapping("/info/{userId}/{resumeId}")
    @ApiOperation(value = "根据用户id和简历id得到简历信息")
    public Result<UserResumesInfoUnIncludeOnlineResumesVo> getUserResumeInfoByUserIdAndResumeId(
            @PathVariable Long resumeId,
            @PathVariable Integer userId) {
        log.info("{}, {}", userId, resumeId);
        return Result.success(resumeService.getUserResumeInfoByUserIdAndResumeId(userId, resumeId));
    }


    /***
    * @Author: CK
    * @Date: 2024/3/1
    * @Description: 添加简历基本信息
    */
    @PostMapping
    @ApiOperation(value = "添加简历基本信息")
    public Result<Map<String, Object>> insertUserBaseInfo(@RequestBody LResume resume) {
        log.info("需要添加的简历信息: {}", resume);
        return Result.success(resumeService.insertUserBaseInfo(resume));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/7
    * @Param: [resume]
    * @Description: 修改简历基本信息
    */
    @PutMapping
    @ApiOperation(value = "修改简历基本信息")
    public Result<Boolean> modifyUserResumeBaseInfo(@RequestBody LResume resume) {
        log.info("需要修改的简历信息: {}", resume);
        return Result.success(resumeService.updateById(resume));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/1
    * @Param: [file, id]
    * @Description: 添加简历头像
    */
    @PostMapping("/photo/{id}/{userId}")
    @ApiOperation(value = "添加简历头像")
    public Result<Boolean> insertUserBaseAvatarById(
            @RequestPart MultipartFile file,
            @PathVariable("id") Long id,
            @PathVariable("userId") Integer userId) throws IOException {
        log.info("用户id ====> {}, 文件信息: {}", id, file);
        //创建一个临时文件
        File tempFile = File.createTempFile("minio_user_resume", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        String filename = file.getOriginalFilename();

        log.info("{}, {}", localFilePath, filename);
        return Result.success(resumeService.insertUserBaseAvatarById(localFilePath, filename, id, userId));
    }

    /**
    * @Author: CK
    * @Date: 2024/3/3
    * @Param: [file, id]
    * @Description: 添加简历文件
    */
    @PostMapping("/file/{id}")
    @ApiOperation(value = "添加简历文件")
    public Result<Boolean> insertUserResumeFile(@RequestPart MultipartFile file, @PathVariable("id") Integer id) throws IOException {
        log.info("需要上传的简历信息: {}: {}", id, file);

        File tempFile = File.createTempFile("minio_user_resume_file", ".temp");
        file.transferTo(tempFile);

        String path = tempFile.getAbsolutePath();

        String filename = file.getOriginalFilename();

        long size = file.getSize();

        log.info("解析结果: {}, {}, {}", path, size, filename);
        return Result.success(resumeService.insertUserResumeFile(path, filename, size, id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/4
    * @Param: [id]
    * @Description: 根据在线简历id下载简历
    */
    @GetMapping("/file/online/{id}")
    @ApiOperation(value = "根据在线简历id下载简历")
    public void downloadUserFileById(@PathVariable String id, HttpServletResponse response) {
        log.info("需要下载的简历id ===> {}" , id);
        resumeService.downloadUserFileById(id, response);
    }

    /***
    * @Author: CK
    * @Date: 2024/3/5
    * @Param: [id]
    * @Description: 根据id删除用户的在线简历文件
    */
    @DeleteMapping("/file/{id}")
    @ApiOperation(value = "根据id删除用户的在线简历文件")
    public Result<Boolean> deleteUserOnlineResumeFileById(@PathVariable("id") String id) {
        log.info("需要删除的在线简历id ====> {}", id);
        return Result.success(resumeService.deleteUserOnlineResumeFileById(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/10
    * @Param: [id]
    * @Description: 根据简历id删除简历信息
    */
    @DeleteMapping("/remove/{id}")
    @ApiOperation(value = "根据简历id删除简历信息")
    public Result<Boolean> deleteUserResumeById(@PathVariable Long id) {
        log.info("需要删除简历的id ===> {}", id);
        return Result.success(resumeService.deleteUserResumeById(id));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/5
    * @Param: [edu]
    * @Description: 修改或添加用户简历的教育经历信息
    */
    @PostMapping("/eduOpera")
    @ApiOperation(value = "修改或添加用户简历的教育经历信息")
    public Result<Boolean> modifyOrSaveUserResumeEduExperienceInfo(@RequestBody LResumeEducateExperience edu) {
        log.info("需要进行操作的信息: {}", edu);
        return Result.success(resumeService.modifyOrSaveUserResumeEduExperienceInfo(edu));
    }

    /***
     * @Author: CK
     * @Date: 2024/3/5
     * @Param: [edu]
     * @Description: 修改或添加用户简历的实习经历信息
     */
    @PostMapping("/praOpera")
    @ApiOperation(value = "修改或添加用户简历的实习经历信息")
    public Result<Boolean> modifyOrSaveUserResumePraExperienceInfo(@RequestBody LResumePracticeExperience pra) {
        log.info("需要进行操作的信息: {}", pra);
        return Result.success(resumeService.modifyOrSaveUserResumePraExperienceInfo(pra));
    }

    /***
     * @Author: CK
     * @Date: 2024/3/5
     * @Param: [edu]
     * @Description: 修改或添加用户简历的项目经历信息
     */
    @PostMapping("/proOpera")
    @ApiOperation(value = "修改或添加用户简历的项目经历信息")
    public Result<Boolean> modifyOrSaveUserResumeProExperienceInfo(@RequestBody LResumeProjectExperience pro) {
        log.info("需要进行操作的信息: {}", pro);
        return Result.success(resumeService.modifyOrSaveUserResumeProExperienceInfo(pro));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/10
    * @Param: [file, id, userId]
    * @Description: 编辑用户头像
    */
    @PostMapping("/photo/modify/{id}/{userId}")
    @ApiOperation(value = "编辑用户头像")
    public Result<Boolean> modifyUserResumeBaseAvatarById(
            @RequestPart MultipartFile file,
            @PathVariable("id") Long id,
            @PathVariable("userId") Integer userId) throws IOException {

        log.info("用户简历id ====> {}, {}, 文件信息: {}", id, userId, file);
        //创建一个临时文件
        File tempFile = File.createTempFile("minio_user_resume_modify", ".temp");
        file.transferTo(tempFile);

        String localFilePath = tempFile.getAbsolutePath();

        String filename = file.getOriginalFilename();

        log.info("{}, {}", localFilePath, filename);
        return Result.success(resumeService.modifyUserResumeBaseAvatarById(localFilePath, filename, id, userId));
    }

    /***
    * @Author: CK
    * @Date: 2024/3/11
    * @Param: [honorAward]
    * @Description: 修改用户简历荣誉奖项
    */
    @PostMapping("/honor/award/modify")
    @ApiOperation(value = "修改用户简历荣誉奖项")
    public Result<Boolean> modifyUserResumeHonorAward(@RequestBody ResumeHonorAwardDto honorAward) {
        log.info("添加/修改用户简历荣誉奖项 ===> {}", honorAward);
        return Result.success(resumeService.modifyUserResumeHonorAward(honorAward));
    }

    /***
     * @Author: CK
     * @Date: 2024/3/11
     * @Param: [hobbyDto]
     * @Description: 修改用户简历个人兴趣与自我评价
     */
    @PostMapping("/hobby/modify")
    @ApiOperation(value = "修改用户简历个人兴趣与自我评价")
    public Result<Boolean> modifyUserResumeHobby(@RequestBody ResumeHobbyDto hobbyDto) {
        log.info("添加/修改用户修改用户简历个人兴趣与自我评价 ===> {}", hobbyDto);
        return Result.success(resumeService.modifyUserResumeHobby(hobbyDto));
    }

    
}